﻿using PLCConsole.PLCSerStation;
using StationSer;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

namespace PLCConsole.PLCConsole
{
    public partial class FPLCInfo_ADP2203VX : Form
    {
        List<AGauge> tIOPorts { get; } = new List<AGauge>();
        List<AGauge> tADPorts { get; } = new List<AGauge>();
        protected Station_ADP2203VX tStationADP2203 { get; private set; } = null;

        public FPLCInfo_ADP2203VX(Station_ADP2203VX SourStation)
        {
            InitializeComponent(); 
            tStationADP2203= SourStation;
            tIOPorts.Add(aGaugeX1);
            tIOPorts.Add(aGaugeX2);
            tIOPorts.Add(aGaugeX3);
            tIOPorts.Add(aGaugeX4);
            tIOPorts.Add(aGaugeX5);
            tIOPorts.Add(aGaugeX6);
            tIOPorts.Add(aGaugeX7);
            tIOPorts.Add(aGaugeX8);
            tIOPorts.Add(aGaugeX9);
            tIOPorts.Add(aGaugeX10);
            tIOPorts.Add(aGaugeX11);
            tIOPorts.Add(aGaugeX12);

            tADPorts.Add(aGaugeAD1);
            tADPorts.Add(aGaugeAD2);
            tADPorts.Add(aGaugeAD3);
            tADPorts.Add(aGaugeAD4);
            tADPorts.Add(aGaugeAD5);
            tADPorts.Add(aGaugeAD6);
            tADPorts.Add(aGaugeAD7);
            tADPorts.Add(aGaugeAD8);
            tADPorts.Add(aGaugeAD9);
        }

        private void tDeamon_Tick(object sender, EventArgs e)
        {

            if (tStationADP2203 == null)
            {
                labelLastAction.Text = "";
                textBoxStaID.Text = "";
            }
            if ((tStationADP2203 != null) && tStationADP2203.HasFlashRegs)
            {
                Text = "ADP2203[" + tStationADP2203.StaID + "]";
                textBoxStaID.Text = tStationADP2203.StaID;
                for (int i = 0; i < 12; i++)
                {
                    tIOPorts[i].Value = tStationADP2203.getXValue(i) ? 1 : 0;

                    if (tIOPorts[i].Value == 0)
                        tIOPorts[i].NeedleColor2 = Color.Red;
                    else
                        tIOPorts[i].NeedleColor2 = Color.Green;
                }
                for (int i = 0; i < 9; i++)
                {
                    tADPorts[i].Value = tStationADP2203.getADCValue(i) / 1000;
                    tADPorts[i].GaugeLabels[1].Text = tADPorts[i].Value.ToString("0.00") + "mA";
                }
                aGaugeVCC.Value = tStationADP2203.VCC_mV / 1000;
                aGaugeVCC.GaugeLabels[1].Text = aGaugeVCC.Value.ToString("0.00") + "V";
                labelLastAction.Text = tStationADP2203.LastActiveTime.ToString("HH:mm:ss.fff");
            }
            for (int i = 0; i < 12; i++)
                if (tIOPorts[i].Value == 0)
                    tIOPorts[i].NeedleColor2 = Color.Red;
                else
                    tIOPorts[i].NeedleColor2 = Color.Green;
        }

        private void FPLCInfo_ADP2203VX_FormClosed(object sender, FormClosedEventArgs e)
        {
            tStationADP2203 = null;
        }
    }
}
